package one.stand.service.impl;

import one.stand.mapper.UserViewMapperExt;
import one.stand.model.UserView;
import one.stand.service.UserViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author cjr
 */
@Service
public class UserViewServiceImpl implements UserViewService {
    @Autowired
    private UserViewMapperExt userViewMapperExt;

    @Override
    @Async("asyncServiceExecutor")
    public void save(Integer userId, Integer type) {
        UserView userView = get(userId, type);
        Date now = new Date();
        if (Objects.nonNull(userView)) {
            UserView userViewUpdate = new UserView();
            userViewUpdate.setId(userView.getId());
            userViewUpdate.setEndTime(now);
            userViewUpdate.setUpdateTime(now);
            userViewMapperExt.updateByPrimaryKeySelective(userViewUpdate);
        } else {
            userView = new UserView();
            userView.setUserId(userId);
            userView.setType(type);
            userView.setEndTime(now);
            userView.setCreateTime(now);
            userViewMapperExt.insertSelective(userView);
        }
    }

    @Override
    public UserView get(Integer userId, Integer type) {
        return userViewMapperExt.select(userId, type);
    }
}
